#include "For.h"


For::For(Assignable *_var, Expr *_first, Expr *_last, Expr *_step, int _ln)
    : var(_var), first(_first), last(_last), step(_step), Stmt(_ln)
{
}


For::~For(void)
{
    delete step;
    delete last;
    delete first;
    delete var;
}

int For::Eval()
{
    int nFirstVal, nLastVal, nStepVal;
    float fFirstVal, fLastVal, fStepVal;
    if (var->Type() == INT)
    {
        nFirstVal = first->EvalInt();
        var->SetValue(nFirstVal);
        if (NULL != step)
        {
            nStepVal = step->EvalInt();
            var->SetStep(nStepVal);
        }
        else
        {
            var->SetStep(1);
        }
    }
    else if (var->Type() == FLT)
    {
        fFirstVal = first->EvalFlt();
        var->SetValue(fFirstVal); 
        if (NULL != step)
        {
            fStepVal = step->EvalFlt();
            var->SetStep(fStepVal);
        }
        else
        {
            var->SetStep(1.0F);
        }
    }
    
    if (var->Type() == INT)
    {
        nLastVal = last->EvalInt();
        var->SetMaxValue(nLastVal);
    }
    else if (var->Type() == FLT)
    {
        fLastVal = last->EvalFlt();
        var->SetMaxValue(fLastVal);
    }
    
    return -1;
}
